package com.example.teamtribe.utils;

import android.Manifest;
import android.support.annotation.RequiresPermission;
import android.util.Log;

import java.io.File;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class HttpUtil {

    private static HttpUtil instance;
    private OkHttpClient client;

    public static String getFailureMeg() {
        return "连接失败，请检查网络设置";
    }

    public static String getCodeMeg(int code) {
        switch (code) {
            case 0:return "请求成功";
            case -1:return "服务器异常";
            case 1:return "参数不合法";
            case 2:return "用户名不存在或密码错误";
            case 4:return "用户名已存在";
            case 5:return "尚未登录";
            case 6:return "下一次发送邮件的等待时间还没过";
            case 7:return "URL无效";
            case 8:return "已经验证过邮箱";
            case 9:return "该邮箱已经被某个帐号绑定";
            case 10:return "已经获取过验证邮件，只是还没点击链接";
            case 11:return "无法连接到教务网站，可能是教务网站崩了";
            case 12:return "教务网站用户名或密码错误";
            case 13:return "教务网站验证码错误";
            case 14:return "教务网站未知错误";
            case 15:return "已经验证过手机号码";
            case 16:return "手机号码已经某个帐号被绑定";
            case 17:return "短信验证码错误";
            case 18:return "今日已经签到";
            case 19:return "今日没有签到";
            case 20:return "一个用户不能同时是某team的会长和管理员或多次设置为管理员";
            case 21:return "权限不够";
            case 22:return "管理员数目到达上限";
            case 23:return "该学校该社团名字已经被使用";
            case 24:return "Team 不存在";
            case 25:return "Post 不存在";
            case 26:return "社团加入申请不存在" ;
            case 27:return "社团加入申请已经被（其他管理员）处理" ;
            case 28:return "已经是 team 的成员" ;
            case 29:return "（设置管理员或退出时不是 team ）的成员" ;
            case 30:return "会长不会退出 team" ;
            case 31:return "相册不存在";
            case 32:return "相册名字已经存在";
            default:return "未知错误";
        }
    }

    private synchronized static HttpUtil getInstance() {
        if (instance == null) {
            instance = new HttpUtil();
        }
        return instance;
    }

    private HttpUtil() {
        client = new OkHttpClient.Builder()
                .connectTimeout(1, TimeUnit.MINUTES)
                .writeTimeout(10, TimeUnit.MINUTES)
                .readTimeout(30, TimeUnit.MINUTES)
                .build();
    }

    @RequiresPermission(Manifest.permission.INTERNET)
    public static void post(String url, Map<String, String> params, Callback callback) {
        Request request = new Request.Builder()
                .url(url)
                .post(buildBody(params))
                .build();
        getInstance().client.newCall(request).enqueue(callback);
    }

    @RequiresPermission(Manifest.permission.INTERNET)
    public static void post(String url, Map<String, String> params, Map<String, File> files, Callback callback) {
        Request request = new Request.Builder()
                .url(url)
                .post(buildBody(params, files))
                .build();
        getInstance().client.newCall(request).enqueue(callback);
    }

    private static RequestBody buildBody(Map<String, String> params) {
        FormBody.Builder builder = new FormBody.Builder();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            builder.add(entry.getKey(), entry.getValue());
            Log.d(entry.getKey(), entry.getValue());
        }
        return builder.build();
    }

    private static RequestBody buildBody(Map<String, String> params, Map<String, File> files) {
        MultipartBody.Builder builder = new MultipartBody.Builder();
        builder.setType(MultipartBody.FORM);
        for (Map.Entry<String, String> entry : params.entrySet()) {
            builder.addFormDataPart(entry.getKey(), entry.getValue());
        }
        for (Map.Entry<String, File> entry : files.entrySet()) {
            builder.addFormDataPart(entry.getKey(), entry.getValue().getName(),
                    RequestBody.create(MediaType.parse("application/octet-stream"), entry.getValue()));
        }
        return builder.build();
    }
}

